<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				relateCacheKey:'bro-th-tsampleBorrowReturn-relate-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			verifySubDel(){
				let _that = this
				if(_that.subEditorObj['productInfo'].workList.length == 1){
					errorModal(_that,'提示信息','删除失败！至少需要保留一行数据',()=>{
					})
					return false
				}else{
					return true
				}
			},
			formSubmit(){
				let _that = this
				_that.om.formSave("tsampleBorrowReturn/addTsampleBorrowReturn",_that.formdata,_that)
			},
			getCodeInfo(code){
				let _that = this
				_that.om.request(
					"tsampleBorrowDelay/getCodeInfo",
					{
						code:code
					}
				).then(res=>{
					if(_that.subEditorObj['productInfo']){
						_that.subEditorObj['productInfo'].workList =  res.value
					}
				})
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
							url:'tsampleBorrowReturn/getInfo',
							param:{
								id:fileId
							},
							method:'POST'
						})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title1","relateCode",{
						link:'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({}),
							url:"tsampleBorrowDelay/getCodeList",
							initload:true,
							namefield:'code/zuserName',
							namesplit:'/',
							notefield:'productName',
							cachename:_that.relateCacheKey
						}
					},null,mainData)
					if(_that.subEditorObj['productInfo']){
						let att = {
							showAdd:false
						}
						let configData = _that.subConfigData['productInfo']
						if(hasEditor("productInfo",'productName',configData)){
							att.titleTemplate = '<%= productName%>'
						}
						changeEditor("productInfo",'objStr',att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[1].value.tsampleBorrowReturn){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tsampleBorrowReturn[index])
								_that.$set(_that.formdata,index,res[1].value.tsampleBorrowReturn[index])
							}
						}
						if(_that.subEditorObj['productInfo']){
							let dataList = res[1].value.tsampleBorrowReturn_data
							_that.subEditorObj['productInfo'].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr',JSON.stringify(dataList))
							_that.$set(_that.formdata,'objStr',JSON.stringify(dataList))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.relateCacheKey,
					success:(res)=>{
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let data = res.data
							formdata.relateCode = data.code
							formdata.customerCodes = data.customerCodes
							formdata.customerName = data.customerName
							formdata.customerAddress = data.customerAddress
							formdata.customerUser = data.customerUser
							formdata.customerMobile = data.customerMobile
							formdata.customerMoney = data.customerMoney
							formdata.customerUserName = data.customerUserName
							formdata.remark = data.remark
							_that.getCodeInfo(data.code)
							uni.removeStorage({
								key:_that.relateCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
